import axios, { AxiosInstance } from "axios";
import {
  type Config,
  CrawlerFileUtils,
  getRandomIP,
  getRandomUserAgent,
} from "../../utils/baseUtils.js";

export interface PageAttributes {
  [key: string]: number | undefined; // 假设所有属性都是number或undefined，根据实际情况可以调整
}
export interface LastPageMap {
  [key: string]: PageAttributes; // 每个键对应一个属性集
}

type makaConfig = Config & {
  lastTokenMap: string[]; // 保存上一次的token
  lastPageMap: LastPageMap; // 保存上一次的page
};
const fileUtils = new CrawlerFileUtils<makaConfig>(import.meta.url);

const axiosInstance: AxiosInstance = axios.create({
  timeout: 10000, // 10秒超时
});
axiosInstance.interceptors.request.use((config) => {
  config.headers["User-Agent"] = getRandomUserAgent();
  config.headers["X-Forwarded-For"] = getRandomIP();
  config.headers["X-Real-IP"] = getRandomIP();
  return config;
});

const getConfig = async () => {
  const config = fileUtils.config;
  const { lastPageMap, lastTokenMap } = config;
  return { lastPageMap, lastTokenMap };
};

export { axiosInstance, fileUtils, getConfig };
